﻿using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace Soul.Common.WCFExtensions
{
    public class UnityBehaviorAttribute : Attribute, IContractBehavior
    {
        public string ContainerName
        { get; set; }

        #region IContractBehavior Members

        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        { }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        { }

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            System.Diagnostics.Debug.WriteLine("InstanceProvider");

            dispatchRuntime.InstanceProvider = new UnityInstanceProvider(contractDescription.ContractType, this.ContainerName);
        }

        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        { }

        #endregion
    }
}
